package com.example.avue.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.avue.entity.Visual;
import com.example.avue.entity.VisualConfig;
import com.example.avue.service.VisualConfigService;
import com.example.avue.service.VisualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/visual")
public class VisualController {
    @Autowired
    VisualService visualService;
    @Autowired
    VisualConfigService visualConfigService;
    /**
     * 打开首页获取模板请列表
     * 因为data下还有一层records所以格式使用JSONObject
     * @return
     */
    @RequestMapping("/list")
    public JSONObject list(){
        List<Visual> list = visualService.getList();
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","success");
        object.put("success",true);
        JSONObject dataObject = new JSONObject();
        dataObject.put("records",list);
        object.put("data",dataObject);
        return object;
    }

    @RequestMapping("/detail")
    public JSONObject detail(@RequestParam String id){
        JSONObject object = new JSONObject();
        VisualConfig visualConfig = visualConfigService.getVisualConfigDetail(Long.valueOf(id));
        Visual visual = visualService.getDetail(Long.valueOf(id));
        JSONObject dataObject = new JSONObject();
        dataObject.put("config",visualConfig);
        dataObject.put("visual",visual);
        object.put("data",dataObject);
        return object;
    }
}
